//
//  MyTabBarViewController.m
//  zhuayin
//
//  Created by wj on 16/6/30.
//  Copyright © 2016年 wujian. All rights reserved.
//



@interface MyTabBarViewController ()<UITabBarDelegate,UITabBarControllerDelegate>{

}

@end

@implementation MyTabBarViewController

+(void)initialize{

    // 通过appearance统一设置所有UITabBarItem的文字属性
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    /** 设置默认状态 */
    NSMutableDictionary *norDict = @{}.mutableCopy;
    norDict[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    norDict[NSForegroundColorAttributeName] = CSColorFromRGB(0x999999);
    [tabBarItem setTitleTextAttributes:norDict forState:UIControlStateNormal];
    
    /** 设置选中状态 */
    NSMutableDictionary *selDict = @{}.mutableCopy;
    selDict[NSFontAttributeName] = norDict[NSFontAttributeName];
    selDict[NSForegroundColorAttributeName] = CSColorFromRGB(0x0a0a0a);
    [tabBarItem setTitleTextAttributes:selDict forState:UIControlStateSelected];
    
}

-(id)init{
    
    self = [super init];
    
    if (self) {
        
        _isFromCompleteInfo = NO;
        
        [self setUp];
    }
    
    return self;
}

-(id)initWithCompleteInfo:(BOOL)isFromCompleteInfo{
    
    self = [super init];
    
    if (self) {
        
        _isFromCompleteInfo = isFromCompleteInfo;
        
        [self setUp];
    }
    
    return self;
}


-(void)setUp{

    /*添加子控制器 */
    
    
    

    NSString * MasterType = [[NSUserDefaults standardUserDefaults] objectForKey:@"MasterType"];
    
    if ([MasterType integerValue] == 0) {
        //教练
        /** 排课页面 */
        ArangViewController *arangVC = [[ArangViewController alloc] init];
        [self setUpChildControllerWith:arangVC norImage:[UIImage imageNamed:@"icon_timetabling_pre"] selImage:[UIImage imageNamed:@"icon_timetabling"] title:@"排课"];
        
        /** 首页预课 */
        
        EngageController *engageVC = [[EngageController alloc] init];
        
        [self setUpChildControllerWith:engageVC norImage:[UIImage imageNamed:@"icon_aboutclass_pre"] selImage:[UIImage imageNamed:@"icon_aboutclass"] title:@"约课"];
        
        /** 圈子 */
        CicleViewController *cicleVC = [[CicleViewController alloc] init];
        [self setUpChildControllerWith:cicleVC norImage:[UIImage imageNamed:@"icon_circle_pre"] selImage:[UIImage imageNamed:@"icon_circle"]title:@"圈子"];
        
        /** 消息 */
        MessageViewController *msgVC = [[MessageViewController alloc] init];
        [self setUpChildControllerWith:msgVC norImage:[UIImage imageNamed:@"icon_news_pre"] selImage:[UIImage imageNamed:@"icon_news"]title:@"消息"];
        
        /** 我的 */
        [self setUpChildControllerWith:[[MyViewController alloc] init] norImage:[UIImage imageNamed:@"icon_persona_pre"] selImage:[UIImage imageNamed:@"icon_persona"] title:@"我的"];
    }else
    {
        //师傅
        
        
        /** 圈子 */
        CicleViewController *cicleVC = [[CicleViewController alloc] init];
        [self setUpChildControllerWith:cicleVC norImage:[UIImage imageNamed:@"icon_circle_pre"] selImage:[UIImage imageNamed:@"icon_circle"]title:@"圈子"];
        
        /** 消息 */
        MessageViewController *msgVC = [[MessageViewController alloc] init];
        [self setUpChildControllerWith:msgVC norImage:[UIImage imageNamed:@"icon_news_pre"] selImage:[UIImage imageNamed:@"icon_news"]title:@"消息"];
        
        /** 我的 */
        [self setUpChildControllerWith:[[MyViewController alloc] init] norImage:[UIImage imageNamed:@"icon_persona_pre"] selImage:[UIImage imageNamed:@"icon_persona"] title:@"我的"];
    }
    
    
    MyTabBar *tabBar = [[MyTabBar alloc] init];
    
    tabBar.delegate = self;
    
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    // 修改tabBar为自定义tabBar
    [self setValue:tabBar forKey:@"tabBar"];
    
    //[self.tabBar setBackgroundImage:[UIImage imageNamed:@"tabBackImage"]];
    
    self.tabBar.barTintColor = CSWhiteColor;//背景颜色
    
    self.tabBar.translucent = NO;
    
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextSetFillColorWithColor(context, [LineBackGroundColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.delegate = self;
    
    [self loadData];
    
    
    
   
        
        
        
    
    
}

- (void)setUpChildControllerWith:(UIViewController *)childVc norImage:(UIImage *)norImage selImage:(UIImage *)selImage title:(NSString *)title
{
    
    childVc.title = title;
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [norImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UserNavigationController *nav = [[UserNavigationController alloc] initWithRootViewController:childVc];
    
    nav.navigationBar.translucent = NO;
    
    [self addChildViewController:nav];
    
    
    
    
}

#pragma mark - 获取个人信息判断绑定问题
-(void)loadData{
    
    NSUserDefaults *userDetault = [NSUserDefaults standardUserDefaults];
    
    NSString *userId = [userDetault objectForKey:@"UserID"];
    
    NSString *url =[NSString stringWithFormat:@"%@/%@",Get_Users,userId];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    //获取本地数据
    ApplyShops *shop1 = [ApplyShops shop];
    
    [HTTPTool GET:url parameters:params
     
          success:^(id responseObject){
              
              NSLog(@"%@",responseObject);
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSDictionary *dict = [responseObject objectForKey:@"ApplyShops"];
                  
                  
                  
                  
                  ApplyShops * shop = [ApplyShops mj_objectWithKeyValues:dict];
                  
                  [ApplyShops saveShop:shop];
                  
                  [self handleData:shop];
                  
                  
              }else
              {
                  [self handleData:shop1];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self handleData:shop1];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

-(void)handleData:(ApplyShops *)shop
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    if (shop) {
        
        
        
        
        [userDefault setBool:YES forKey:@"isCompleteInfo"];
        
        if ([shop.Status integerValue] == 1) {
            //审核中
            [JHSysAlertUtil presentAlertViewWithTitle:@"温馨提示" message:@"暂时无法使用，等待后台审核。" confirmTitle:@"退出" handler:^{
                exit(0);
            }];
            
            
            
        }
        
        
    }
    else{
        
        [userDefault setBool:NO forKey:@"isCompleteInfo"];
        
        
        //读用户类型
        NSString *MasterType = [userDefault valueForKey:@"MasterType"];
        
        __weak typeof(self)weakSelf = self;
        
        
        //场馆，门派绑定
        
        if ([MasterType integerValue] == 1) {
            //师傅
            
            CZQSelctChoseView *SelctChoseView = [[CZQSelctChoseView alloc] init];
            
            SelctChoseView.urlString = Get_GetAllSectListData;
            
            SelctChoseView.commitBlock = ^(NSString *str) {
                
                CZQSelctChoseView *SelctChoseView1 = [[CZQSelctChoseView alloc] init];
                
                NSMutableDictionary *param = [NSMutableDictionary dictionary];
                
                
                SelctChoseView1.pandID = str;
                
                SelctChoseView1.urlString = @"";
                
                
                SelctChoseView1.commitBlock = ^(NSString *str) {
                    
                    
                    
                };
                
                [SelctChoseView1 showWithAnimation];
                
                
            };
            
            [SelctChoseView showWithAnimation];
            
        }else
        {
            //教练
            
            CZQShopBandChoseView *sourceAlertView = [[CZQShopBandChoseView alloc] init];
            
            sourceAlertView.urlString = Get_Brand;
            
            sourceAlertView.commitBlock = ^(NSString *str) {
                
                CZQShopBandChoseView *shop = [[CZQShopBandChoseView alloc] init];
                
                NSMutableDictionary *param = [NSMutableDictionary dictionary];
                
                
                shop.pandID = str;
                
                shop.urlString = [NSString stringWithFormat:@"%@%@",Get_ShopList,str];
                
                
                shop.commitBlock = ^(NSString *str) {
                    
                    
                    
                };
                
                [shop showWithAnimation];
                
                
            };
            
            [sourceAlertView showWithAnimation];
            
        }
        
    }
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    NSLog(@"selectIndex = %lu",(unsigned long)self.selectedIndex);
    
}

//- (void)pushToChatViewControllerWith:(IMAUser *)user
//{
//    UserNavigationController *curNav = (UserNavigationController *)[[self viewControllers] objectAtIndex:self.selectedIndex];
//    if (self.selectedIndex == 0)
//    {
//        // 选的中会话tab
//        // 先检查当前栈中是否聊天界面
//        NSArray *array = [curNav viewControllers];
//        for (UIViewController *vc in array)
//        {
//            if ([vc isKindOfClass:[IMAChatViewController class]])
//            {
//                // 有则返回到该界面
//                IMAChatViewController *chat = (IMAChatViewController *)vc;
//                [chat configWithUser:user];
//                //                chat.hidesBottomBarWhenPushed = YES;
//                [curNav popToViewController:chat animated:YES];
//                return;
//            }
//        }
//#if kTestChatAttachment
//        // 无则重新创建
//        ChatViewController *vc = [[CustomChatUIViewController alloc] initWith:user];
//#else
//        ChatViewController *vc = [[IMAChatViewController alloc] initWith:user];
//#endif
//        
//        if ([user isC2CType])
//        {
//            TIMConversation *imconv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:user.userId];
//            if ([imconv getUnReadMessageNum] > 0)
//            {
//                [vc modifySendInputStatus:SendInputStatus_Send];
//            }
//        }
//        
//        vc.hidesBottomBarWhenPushed = YES;
//        [curNav pushViewController:vc animated:YES];
//    }
//    else
//    {
//        NavigationViewController *chatNav = (NavigationViewController *)[[self viewControllers] objectAtIndex:0];
//        
//#if kTestChatAttachment
//        // 无则重新创建
//        ChatViewController *vc = [[CustomChatUIViewController alloc] initWith:user];
//#else
//        ChatViewController *vc = [[IMAChatViewController alloc] initWith:user];
//#endif
//        vc.hidesBottomBarWhenPushed = YES;
//        [chatNav pushViewController:vc animated:YES];
//        
//        [self setSelectedIndex:0];
//        
//        if (curNav.viewControllers.count != 0)
//        {
//            [curNav popToRootViewControllerAnimated:YES];
//        }
//        
//    }
//}

- (UIStatusBarStyle)preferredStatusBarStyle

{
    
//    return UIStatusBarStyleDefault;
    
//    UIStatusBarStyleDefault = 0 黑色文字，浅色背景时使用
    
    return UIStatusBarStyleDefault;
    
}

@end
